from django.db.models.signals import post_save, post_delete
from django.dispatch import receiver
from .models import Exhibit, Exhibitor, Attendance, Schedule

@receiver(post_save, sender=Exhibit)
@receiver(post_delete, sender=Exhibit)
def update_exhibitor_stats(sender, instance, **kwargs):
    """
    当展品被创建、更新或删除时，更新相关参展商的展品数量
    """
    if hasattr(instance, 'exhibitor') and instance.exhibitor:
        exhibitor = instance.exhibitor
        # 更新参展商的展品数量
        exhibits_count = Exhibit.objects.filter(exhibitor=exhibitor).count()
        exhibitor.num_exhibits = exhibits_count
        exhibitor.save(update_fields=['num_exhibits'])

@receiver(post_save, sender=Attendance)
@receiver(post_delete, sender=Attendance)
def update_attendance_stats(sender, instance, **kwargs):
    """
    当参与记录被创建、更新或删除时，可以在这里添加相关处理逻辑
    例如：记录活动参与人数统计、更新观众参与活动次数等
    """
    # 这里可以添加自定义的处理逻辑
    pass

@receiver(post_save, sender=Schedule)
def notify_new_event(sender, instance, created, **kwargs):
    """
    当新活动被创建时，可以在这里添加通知逻辑
    例如：发送邮件通知相关人员、更新首页展示等
    """
    if created:
        # 这里可以添加新活动通知逻辑
        pass 